package service;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import beans.DynaPagedQuery;

/**
 * 主要在页面上调用，比如获取某个sql的执行结果
 * @author wuzh
 * 
 */
public class MyAppToolService {
    /** Hibernate SessionFactory. */
    private SessionFactory sessionFactory;

    /**
     * 执行Hsql语句并返回执行结果，结果为一个AbstractPagedQuery对象，可以用于分页.
     * @param pKey key
     * @return Object
     */
    public Object getHsqlXResluts(String pKey) {
        String[] params = pKey.trim().split("/");
        if (params.length < 2) {
            throw new java.lang.IllegalArgumentException(
                    "must at least provide two sql for query count and detail records "
                            + pKey);
        }
        // 查询总数的hsql
        String countHsql = params[0];
        // 查询细目的hsql
        String detailHsql = params[1];
        // itemsPerPage
        int itemsPerPage = 20;
        // 查询从第几条纪录开始
        int pagerOffset = 0;
        if (params.length >= 3) {
            itemsPerPage = Integer.parseInt(params[2]);
        }
        if (params.length >= 4) {
            pagerOffset = Integer.parseInt(params[3]);
        }
        Session session = sessionFactory.getCurrentSession();
        DynaPagedQuery dynaQry = new DynaPagedQuery();
        dynaQry.setPagerOffset(pagerOffset);
        dynaQry.setItemsPerPage(itemsPerPage);
        // 查询总数
        Query qry = session.createQuery(countHsql);
        long totalCount = (Long) qry.uniqueResult();
        dynaQry.setTotalCount(totalCount);
        // 查询细目
        dynaQry.checkPagerOffset();
        qry = session.createQuery(detailHsql);
        qry.setFirstResult(dynaQry.getPagerOffset());
        qry.setMaxResults(itemsPerPage);
        // 设置查询结果集
        dynaQry.setQueryResults(qry.list());
        return dynaQry;
    }

    /**
     * 执行Hsql语句并返回执行结果
     * @param pHsql
     * @return Object
     */
    public Object getHsqlResluts(String pHsql) {
        Session session = sessionFactory.getCurrentSession();
        Query qry = session.createQuery(pHsql);
        return qry.list();
    }

    /**
     * 执行SQL语句并返回执行结果
     * @param sql
     * @return Object(可能是一个数组或单值，具体看sql语句了)
     */
    public Object getSQLResluts(String sql) {
        Session session = sessionFactory.getCurrentSession();
        Query qry = session.createSQLQuery(sql);
        return qry.list();
    }

    /**
     * 查询相关文件列表.
     * @param pKey key
     * @return RelatedFile集合
     */
    public Object getRelatedFileInfos(String pKey) {
        String[] params = pKey.trim().split(":");
        // 相关对象类型
        String topic = params[0];
        // 相关对象id
        String relatedTo = params[1];
        String hsql = "from domain.RelatedFile rfile where rfile.topic=:topic and rfile.relatedTo = :relatedTo";
        Session session = sessionFactory.getCurrentSession();
        Query qry = session.createQuery(hsql);
        qry.setString("topic", topic);
        qry.setString("relatedTo", relatedTo);
        return qry.list();
    }

    /**
     * set sessionFactory
     * @param pSessionFactory sessionFactory
     */
    public void setSessionFactory(SessionFactory pSessionFactory) {
        sessionFactory = pSessionFactory;
    }
}
